package com.atguigu.cloud.controller;

import com.atguigu.cloud.entities.Pay;
import com.atguigu.cloud.entities.PayDTO;
import com.atguigu.cloud.resp.ResultData;
import com.atguigu.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author yuanx
 * @date 2024/3/3 18:36
 */
@RestController()
@RequestMapping("/pay")
@Slf4j
@Tag(name = "支付微服务模块", description = "支付CRUD")
public class PayController {
    @Resource
    private PayService payService;

    @PostMapping(value = "/add")
    @Operation(summary = "添加", description = "add")
    public ResultData<String> add(@RequestBody Pay pay) {
        System.out.println(pay.toString());
        int i = payService.addPay(pay);
        return ResultData.success(String.format("%s成功插入", i));
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除", description = "delete")
    public ResultData<Integer> deletePay(@PathVariable("id") Integer id) {
        int deletePay = payService.deletePay(id);
        return ResultData.success(deletePay);
    }

    @PutMapping("/update")
    @Operation(summary = "更新", description = "update")
    public ResultData<Integer> updatePay(@RequestBody PayDTO payDTO) {
        Pay pay = new Pay();
        BeanUtils.copyProperties(payDTO, pay);
        int i = payService.updatePay(pay);
        return ResultData.success(i);
    }

    @GetMapping("/getById/{id}")
    @Operation(summary = "通过id查询", description = "getById")
    public ResultData<Pay> selectPayById(@PathVariable("id") Integer id) {

//        if (id == -4) {
//            throw new RuntimeException("不能小于0");
//        }
        if (id == -4) {
            throw new RuntimeException("不能小于0");
        }

        Pay pay = payService.selectById(id);
        return ResultData.success(pay);
    }

    @GetMapping("/getAll")
    @Operation(summary = "查询全部", description = "getAll")
    public ResultData<List<Pay>> selectPagetAllyById() {
        try {
            TimeUnit.SECONDS.sleep(62L);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Pay> pays = payService.selectAllPay();
        return ResultData.success(pays);
    }

    @Value("${server.port}")
    private String port;

    @GetMapping("/getInfoByConsul")
    private  String getInfoByConsul(){
       return "port:"+port;
    }


}
